﻿using System;
using System.Data.Common;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that adds a new business unit to the current domain.
    /// </summary>
    internal class AddUnitCommand : Command
    {
        /// <summary>
        ///   The unit name parameter.
        /// </summary>
        public const string UnitNameParameter = "name";

        /// <summary>
        ///   The unit name parameter (short name).
        /// </summary>
        public const string UnitNameShortParameter = "n";

        /// <summary>
        ///   The unit display name parameter.
        /// </summary>
        public const string UnitDisplayNameParameter = "displayname";

        /// <summary>
        ///   The unit display name parameter (short name).
        /// </summary>
        public const string UnitDisplayNameShortParameter = "dn";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "addunit"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.AddUnitCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.AddUnitCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            // If the unit was not specified
            if (!context.ParameterValueIsNotEmpty(UnitNameParameter, UnitNameShortParameter))
            {
                Console.WriteLine(Messages.AddUnitCommandNotAllParametersSpecifiedError);
                return;
            }

            var domain = context.CurrentDomain;

            if (domain == null) return;

            // If the current user has no permission to perform this action
            if (!domain.CurrentUser.CanChangeSecurity)
            {
                Console.WriteLine(Messages.NeedCanChangeSecurityPermissionMessage);
                return;
            }

            // The unit name
            var unitName = context.GetParameterValue(UnitNameParameter, UnitNameShortParameter);

            try
            {
                var unit = domain.GetUnit(unitName) ?? domain.AddUnit(unitName);

                // If the unit display name was specified
                if (context.ParameterValueIsNotEmpty(UnitDisplayNameParameter, UnitDisplayNameShortParameter))
                    unit.DisplayName = context.GetParameterValue(UnitDisplayNameParameter, UnitDisplayNameShortParameter);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }
            catch (DbException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }
    }
}
